
<template>
  <div align="center">
    <el-form
      ref="f1"
      :model="form"
      :rules="rules"
      label-width="100px"
      style="width: 40%"
    >
      <el-form-item label="账号" prop="name">
        <el-input v-model="form.name" placeholder="请输入账号" />
      </el-form-item>
      <el-form-item label="电话号码" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入电话号码" />
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入电子邮箱" />
      </el-form-item>
      <el-form-item label="验证码" prop="yan">
        <el-input v-model="form.yan" placeholder="请输入验证码" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit()">提交注册信息</el-button>
        <el-button type="info">重置</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        name: "",
        phone: "",
        email: "",
        yan: "",
      },
      rules: {
        name: [
          { required: true, message: "账号必填", trigger: "blur" },
          {
            pattern: /^\w{6,10}$/,
            message: "6~10位字符包含,包含:数字/字母/下划线",
            trigger: "blur",
          },
        ],
        phone: [
          { required: true, message: "电话号码必填", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
        email: [
          { required: true, message: "邮箱必填", trigger: "blur" },
          {
            pattern:
              /^((?!-)[\w-]+\.)*(?!-)[\w-]+(?<!-)@((?!-)[\w-]+\.)+[A-Za-z]{2,6}$/,
            message: "邮箱格式不正确",
            trigger: "blur",
          },
        ],
        yan: [
          { required: true, message: "验证码必填", trigger: "blur" },
          {
            pattern: this.yan == this.yan,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submit() {
      //点击注册按钮后执行，收集所有数据，发请求
      console.log(this.form);
      this.$refs["f1"].validate((value) => {
        if (value) { //验证成功 valid==true
          console.log(this.form);
        } else {  //验证失败 valid==false
          console.warn("验证失败，检查表单项");
          console.info("验证失败，检查表单项");
          console.error("验证失败，检查表单项");
          console.log("验证失败，检查表单项");
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>